package com.example.himalaya.fragments;

import static com.example.himalaya.base.BaseApplication.getAppContext;

import android.content.Intent;
import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.PlayerActivity;
import com.example.himalaya.R;
import com.example.himalaya.adapters.TrackListRvAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.interfaces.IHistoryPresenterCallback;
import com.example.himalaya.presenters.HistoryPresenter;
import com.example.himalaya.presenters.PlayerPresenter;
import com.example.himalaya.views.ConfirmCheckBoxDialog;
import com.example.himalaya.views.UILoader;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

/**
 * 历史Fragment片段
 */
public class HistoryFragment extends BaseFragment implements IHistoryPresenterCallback {
    private static final String TAG = "HistoryFragment";

    private UILoader mUiLoader;
    private TrackListRvAdapter mTrackListRvAdapter;
    private HistoryPresenter mHistoryPresenter;
    private Track mCurrentClickHistoryItem;

    @Override
    protected View onSubViewLoaded(LayoutInflater inflater, ViewGroup container) {
        //view加载完成
        FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_histroy, container, false);
        if (mUiLoader==null) {

            mUiLoader = new UILoader(getAppContext()) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSussessView(container);
                }


                @Override
                protected View getDataEmptyView() {
                    View emptyView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_empty_view, this, false);
                    TextView emptyViewText = emptyView.findViewById(R.id.empty_view_text);
                    emptyViewText.setText("没有历史记录呢，赶紧去听吧!");
                    return emptyView;
                }
            };
        }else {
            if(mUiLoader.getParent() instanceof ViewGroup) {
                ((ViewGroup) mUiLoader.getParent()).removeView(mUiLoader);
            }
        }

        mHistoryPresenter = HistoryPresenter.getHistoryPresenter();
        mHistoryPresenter.registerViewCallBack(this);
        mHistoryPresenter.listHistories();

        view.addView(mUiLoader);

        //返回view给父类Fragment片段
        return view;
    }

    private View createSussessView(ViewGroup container) {
        View successView = LayoutInflater.from(container.getContext()).inflate(R.layout.fragment_history_item, container, false);
        TwinklingRefreshLayout refreshLayout = successView.findViewById(R.id.recommend_refreshLayout);
        refreshLayout.setPureScrollModeOn(true);

        RecyclerView historyList = successView.findViewById(R.id.history_list);
        historyList.setLayoutManager(new LinearLayoutManager(container.getContext()));
        historyList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(),2);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 2);
                outRect.left=UIUtil.dip2px(view.getContext(),5);
                outRect.right=UIUtil.dip2px(view.getContext(),5);
            }
        });
        mTrackListRvAdapter = new TrackListRvAdapter();

        mTrackListRvAdapter.setOnTrackItemClickListener(new TrackListRvAdapter.onTrackItemClickListener() {
            @Override
            public void OnTrackItemClick(List<Track> tracks, int position) {
                //设置播放器数据
                PlayerPresenter playerPresenter = PlayerPresenter.getPlayerPresenter();
                playerPresenter.setPlayList(tracks,position);

                Log.d(TAG, "OnTrackItemClick: "+position);
                //跳转到播放器界面
                Intent intent = new Intent(getActivity(), PlayerActivity.class);
                startActivity(intent);
            }
        });

        mTrackListRvAdapter.setOnItemLongClickListener(new TrackListRvAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(Track track) {
                mCurrentClickHistoryItem = track;
                ConfirmCheckBoxDialog dialog = new ConfirmCheckBoxDialog(getActivity());
                dialog.setOnDialogActionClickListener(new ConfirmCheckBoxDialog.OnDialogActionClickListener() {
                    @Override
                    public void onCancelClick() {
                        //不用做
                    }

                    @Override
                    public void onConfirmClick(boolean isCheck) {
                        //删除历史
                        if (mHistoryPresenter!=null && mCurrentClickHistoryItem != null) {
                            if (!isCheck) {
                                mHistoryPresenter.delHistory(mCurrentClickHistoryItem);
                            }else {
                                mHistoryPresenter.cleanHistories();
                            }
                        }
                    }
                });
                dialog.show();

            }
        });

        historyList.setAdapter(mTrackListRvAdapter);


        return successView;
    }

    @Override
    public void onHistoriesLoaded(List<Track> tracks) {
        if (tracks==null||tracks.size()==0) {
            mUiLoader.updateUIStatus(UILoader.UIStatus.EMPTY);
        }else {
            //更新数据
            mTrackListRvAdapter.setTrackData(tracks);
            mUiLoader.updateUIStatus(UILoader.UIStatus.SUCCESS);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mHistoryPresenter != null) {
            mHistoryPresenter.unRegisterViewCallBack(this);
            mHistoryPresenter = null;
        }
    }
}
